package com.ningmengs789.live.ui.widget.dialogorPopwindow;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ningmengs789.live.R;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;

public class SalaryAlertDialog implements OnWheelChangedListener {

    private Context context;
    private Dialog dialog;
    private LinearLayout lLayout_bg;
    private TextView txt_title;

    private WheelView agewheel;

    private TextView btn_cancel;
    private TextView btn_confirm;

    private Display display;

    private String[] datas;

    private String dateStr = "";

    public String getDateStr() {
        return dateStr;
    }

    public SalaryAlertDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public SalaryAlertDialog builder() {
        View view = LayoutInflater.from(context).inflate(R.layout.view_age_alert_dialog, null);

        lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);
        txt_title = (TextView) view.findViewById(R.id.txt_title);
        agewheel = (WheelView) view.findViewById(R.id.agewheel);
        btn_cancel = view.findViewById(R.id.btn_cancel);
        btn_confirm = view.findViewById(R.id.btn_confirm);

        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);

        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display.getWidth() * 0.80), ViewGroup.LayoutParams.WRAP_CONTENT));

        setView();

        return this;
    }

    public SalaryAlertDialog setTitle(String title) {
        if (!TextUtils.isEmpty(title)){
            txt_title.setText(title);
        }
        return this;
    }

    public SalaryAlertDialog setData(String[] datas) {
        if (datas != null){
            this.datas = datas;
        }
        return this;
    }

    public SalaryAlertDialog setPositiveButton(String text, final View.OnClickListener listener) {
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                dialog.dismiss();
            }
        });
        return this;
    }

    public SalaryAlertDialog setNegativeButton(String text, final View.OnClickListener listener) {
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                dialog.dismiss();
            }
        });
        return this;
    }

    private void setView() {
        ArrayWheelAdapter arrayWheelAdapter = new ArrayWheelAdapter<String>(context, datas, R.layout.wheel_layout, R.id.wheel_layout_tv);
//        arrayWheelAdapter.setItemResource();
        agewheel.setViewAdapter(arrayWheelAdapter);

        agewheel.setCurrentItem(1);
        dateStr = datas[1];

        //监听
        agewheel.addChangingListener(this);

        //显示的单元格数目
        agewheel.setVisibleItems(3);
    }

    public void show() {
        dialog.show();
    }

    public void dismiss() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == agewheel) {
            dateStr = datas[agewheel.getCurrentItem()];
        }
    }
}

